from django.shortcuts import render_to_response
from heetun.models import User
from django.utils import simplejson
from django.http import HttpResponse
from face.models import Face

def getFaceView(request):
    name = request.GET.get('name', '')
    id = request.GET.get('id', '')
    defualtFace = 'static/img/face.jpg'
    try:
        if name and id:
            face = Face.objects.get(owner__username = name,id=id)
        elif name:
            face = Face.objects.get(owner__username = name)
    except Face.DoesNotExist:
        face = Face(img=defualtFace)
    
    if request.is_ajax():
        if face:
            data = {'name':name,'src':face.img.url}
        else:
            data = {'name':'','src':defualtFace}
        return HttpResponse(simplejson.dumps(data),'application/javascript')
    else:
        if face:
            return HttpResponse(face.img.url)
        else:
            return HttpResponse(defualtFace)
    
